﻿using OpenQA.Selenium;
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System.Threading;

namespace Helpers.ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            //Console.WriteLine(CmdHelper.RunCmd(@"python", @"C:\Users\Administrator\Desktop\test\test.py").Result);
            //Console.WriteLine(CmdHelper.RunCmd(@"C:\Users\Administrator\Desktop\test\test.py", "fdf").Result);
            //Console.WriteLine(CmdHelper.RunCmd(@"ping.exe", "8.8.8.8").Result);

            var MyConfiguration = Helpers.ConfigHelper.CreateConfiguration(
                //jsonFiles: new string[] { "log.json" },
                //secretsId: "bb14c8c9-52ec-423f-b7fa-aa5ccfa290e6"
                );
            Helpers.DiHelper.Services
                //.AddMemoryCache()
                .InjectConfiguration(MyConfiguration)
                .InjectLogging(MyConfiguration);

            //for (int i = 0; i < 10; i++)
            //{
            //    Helpers.MemoryCacheHelper.GetOrCreate("1", it =>
            //    {
            //        Helpers.LogHelper.Logger.LogInformation(DateTime.Now.ToString());
            //        it.AbsoluteExpirationRelativeToNow = TimeSpan.FromDays(1);
            //        return "";
            //    });
            //}

            //string a = "123456789";
            //string b = "123456789";
            //Task.Run(() =>
            //{
            //    lock (a)
            //    {
            //        Helpers.LogHelper.Logger.LogInformation(DateTime.Now.ToString());
            //        System.Threading.Thread.Sleep(10000);
            //    }
            //});

            //Task.Run(() =>
            //{
            //    lock (b)
            //    {
            //        Helpers.LogHelper.Logger.LogInformation(DateTime.Now.ToString());
            //        System.Threading.Thread.Sleep(10000);
            //    }
            //});

            //Parallel.For(0, 10, (i) => {
            //    Helpers.MemoryCacheHelper.GetOrCreate("1", it =>
            //    {
            //        Helpers.LogHelper.Logger.LogInformation(DateTime.Now.ToString());
            //        System.Threading.Thread.Sleep(10000);
            //        it.AbsoluteExpirationRelativeToNow = TimeSpan.FromDays(1);
            //        return "";
            //    });
            //});
            //for (int i = 1; i <= 2; i++)
            //{
            //    var index = i.ToString();
            //    Task.Run(() =>
            //    {
            //        Loop(index, 1000);
            //    });
            //    System.Threading.Thread.Sleep(1000);
            //}

            System.Threading.Thread.Sleep(-1);
        }
        public static void Loop(string index, int count)
        {
            Helpers.SeleniumHelper.Killfirefox();
            try
            {
                Stopwatch stopwatch = Stopwatch.StartNew();
                for (int i = 1; i < count; i++)
                {
                    Stopwatch stopwatch2 = Stopwatch.StartNew();
                    try
                    {
                        var firefox = Helpers.SeleniumHelper.CreateFirefoxDriver(new Options.FirefoxConfig
                        {
                            UseDriver = true,
                            DriverPath = @"C:\firefox",
                            DriverName = "geckodriver.exe",
                            UseBrowser = true,
                            BrowserPath = @"C:\firefox\Firefox\firefox.exe",
                            UseProfile = false,
                            ProfilePath = null,
                            UseProxy = true,
                            proxy = new OpenQA.Selenium.Proxy()
                            {
                                Kind = OpenQA.Selenium.ProxyKind.Direct,
                            },
                            UseHeadless = true,
                            LogLevel = OpenQA.Selenium.Firefox.FirefoxDriverLogLevel.Info,
                        });
                        firefox.Driver.Url = "https://www.baidu.com";
                        firefox.Driver.Dispose();
                        FileHelper.DelectDir(Path.GetTempPath());
                    }
                    catch (Exception ex)
                    {
                        i--;
                        Console.WriteLine(ex);
                    }
                    Console.WriteLine($"---------------------------------------------------------------------------{index}-{i}-{stopwatch2.Elapsed.TotalSeconds:N0}-{stopwatch.Elapsed.TotalSeconds / i:N0}");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
    }
}
